const mongoose = require('mongoose');

const orderSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  orderNo: {
    type: String,
    required: true,
    unique: true
  },
  items: [{
    goods: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Goods',
      required: true
    },
    count: {
      type: Number,
      required: true
    },
    price: {
      type: Number,
      required: true
    },
    specs: [{
      name: String,
      value: String
    }],
    blessing: String
  }],
  totalAmount: {
    type: Number,
    required: true
  },
  address: {
    name: String,
    phone: String,
    province: String,
    city: String,
    district: String,
    detail: String
  },
  status: {
    type: Number,
    default: 0 // 0: 待付款, 1: 待发货, 2: 待收货, 3: 已完成, 4: 已取消
  },
  paymentMethod: String,
  paymentTime: Date,
  deliveryTime: Date,
  completeTime: Date,
  cancelTime: Date,
  remark: String,
  createdAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('Order', orderSchema); 